home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ByteArrayStore.java < prev    next >
Text File  |  1998-09-08  |  6KB  |  191 lines

  1. package com.symantec.itools.util;
  2.  
  3.  
  4. import com.symantec.itools.lang.Machine;
  5. import java.net.*;
  6.  
  7. /**
  8.  * @author Symantec Internet Tools Division
  9.  * @version 1.0
  10.  * @since VCafe 3.0
  11.  */
  12.  
  13. public class ByteArrayStore
  14. {
  15.  
  16.     /**
  17.      * @param barr TODO
  18.      * @param start TODO
  19.      * @since VCafe 3.0
  20.      */
  21.     /* Integer operations */
  22.     public static final int constructInt( int[] barr, int start )
  23.     {
  24.         int j;
  25.         int k;
  26.         int i = 0;
  27.         for ( k = 0, j = Machine.SIZEOF_INT - 1; j > -1; j--, k++ )
  28.         {
  29.             //  i |= barr[ start + j ] << (j*Machine.WIDTHOF_BYTE);
  30.             /*
  31.                 The int mask, ((int)0xFF << (j* Machine.WIDTHOF_BYTE ), is needed because
  32.                 Java does sign aware widening.  So something like (byte)0x80 widens to
  33.                 0xFF80.  Of course this screws things up when you try to OR into i the shifted
  34.                 byte.
  35.             */
  36.             i |= ( barr[ start + k ] << (j* Machine.WIDTHOF_BYTE )) & ((int)0xFF << (j* Machine.WIDTHOF_BYTE ) );
  37.         }
  38.         return i;
  39.     }
  40.  
  41.     /**
  42.      * @param barr TODO
  43.      * @param start TODO
  44.      * @since VCafe 3.0
  45.      */
  46.     public static final int constructInt( byte[] barr, int start )
  47.     {
  48.         int j;
  49.         int k;
  50.         int i = 0;
  51.         for ( k = 0, j = Machine.SIZEOF_INT - 1; j > -1; j--, k++ )
  52.         {
  53.             //  i |= barr[ start + j ] << (j*Machine.WIDTHOF_BYTE);
  54.             /*
  55.                 The int mask, ((int)0xFF << (j* Machine.WIDTHOF_BYTE ), is needed because
  56.                 Java does sign aware widening.  So something like (byte)0x80 widens to
  57.                 0xFF80.  Of course this screws things up when you try to OR into i the shifted
  58.                 byte.
  59.             */
  60.             i |= ( barr[ start + k ] << (j* Machine.WIDTHOF_BYTE )) & ((int)0xFF << (j* Machine.WIDTHOF_BYTE ) );
  61.         }
  62.         return i;
  63.     }
  64.  
  65.     /**
  66.      * @param i TODO
  67.      * @since VCafe 3.0
  68.      */
  69.     public static final byte[] deconstructInt( int i )
  70.     {
  71.         byte[] barr = new byte[ Machine.SIZEOF_INT ];
  72.  
  73.         for ( int k = 0, j = Machine.SIZEOF_INT - 1; j > -1; j--, k++ )
  74.         {
  75.               barr[ k ] = (  (byte)( i >> (j*Machine.WIDTHOF_BYTE) ) ) ;
  76.         }
  77.  
  78.         return barr;
  79.     }
  80.  
  81.     /**
  82.      * @param i TODO
  83.      * @param dest TODO
  84.      * @param index TODO
  85.      * @since VCafe 3.0
  86.      */
  87.     public static final void deconstructIntInto( int i, byte[] dest, int index )
  88.     {
  89.         for ( int k = 0, j = Machine.SIZEOF_INT - 1; j > -1; j--, k++ )
  90.         {
  91.               dest[ index + k ] = (  (byte)( i >> (j*Machine.WIDTHOF_BYTE) ) ) ;
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * @param barr TODO
  97.      * @param start TODO
  98.      * @since VCafe 3.0
  99.      */
  100.     /* long operations */
  101.     public static final long constructLong( int[] barr, int start )
  102.     {
  103.         int j;
  104.         int i;
  105.         long l = 0;
  106.         for ( i = 0, j = Machine.SIZEOF_LONG - 1; j > -1; j--, i++ )
  107.         {
  108.             //  l |= barr[ start + j ] << (j*Machine.WIDTHOF_BYTE);
  109.             /*
  110.                 The int mask, ((int)0xFF << (j* Machine.WIDTHOF_BYTE ), is needed because
  111.                 Java does sign aware widening.  So something like (byte)0x80 widens to
  112.                 0xFF80.  Of course this screws things up when you try to OR into l, that's lower case L, the shifted
  113.                 byte.
  114.             */
  115.             l |= ( barr[ start + i ] << (j* Machine.WIDTHOF_BYTE )) & ((long)0xFF << (j* Machine.WIDTHOF_BYTE ) );
  116.         }
  117.         return l;
  118.     }
  119.  
  120.     /**
  121.      * @param barr TODO
  122.      * @param start TODO
  123.      * @since VCafe 3.0
  124.      */
  125.     public static final long constructLong( byte[] barr, int start )
  126.     {
  127.         int j;
  128.         int i;
  129.         long l = 0;
  130.         for ( i = 0, j = Machine.SIZEOF_LONG - 1; j > -1; j--, i++ )
  131.         {
  132.             //  l |= barr[ start + j ] << (j*Machine.WIDTHOF_BYTE);
  133.             /*
  134.                 The int mask, ((int)0xFF << (j* Machine.WIDTHOF_BYTE ), is needed because
  135.                 Java does sign aware widening.  So something like (byte)0x80 widens to
  136.                 0xFF80.  Of course this screws things up when you try to OR into l, that's lower case L, the shifted
  137.                 byte.
  138.             */
  139.             l |= ( barr[ start + i ] << (j* Machine.WIDTHOF_BYTE )) & ((long)0xFF << (j* Machine.WIDTHOF_BYTE ) );
  140.         }
  141.         return l;
  142.     }
  143.  
  144.     /**
  145.      * @param l TODO
  146.      * @since VCafe 3.0
  147.      */
  148.     public static final byte[] deconstructLong( long l )
  149.     {
  150.         byte[] barr = new byte[ Machine.SIZEOF_LONG ];
  151.  
  152.         for ( int i = 0, j = Machine.SIZEOF_LONG - 1; j > -1; j--, i++ )
  153.         {
  154.               barr[ i ] = (  (byte)( l >> (j*Machine.WIDTHOF_BYTE) ) ) ;
  155.         }
  156.  
  157.         return barr;
  158.     }
  159.  
  160.     /**
  161.      * @param l TODO
  162.      * @param dest TODO
  163.      * @param index TODO
  164.      * @since VCafe 3.0
  165.      */
  166.     public static final void deconstructLongInto( long l, byte[] dest, int index )
  167.     {
  168.         for ( int i = 0, j = Machine.SIZEOF_LONG - 1; j > -1; j--, i++ )
  169.         {
  170.               dest[ index + i ] = (  (byte)( l >> (j*Machine.WIDTHOF_BYTE) ) ) ;
  171.         }
  172.     }
  173.  
  174.     /**
  175.      * @param barr TODO
  176.      * @param start TODO
  177.      * @since VCafe 3.0
  178.      */
  179.     /* InetAddress operations */
  180.     public static final String constructInetAddressString( byte[] barr, int start )
  181.     {
  182.         String ret =
  183.             (
  184.                 ((int)barr[ start ] & 0xFF ) + "." +
  185.                 ((int)barr[ start + 1 ] & 0xFF ) + "." +
  186.                 ((int)barr[ start + 2 ] & 0xFF )  + "." +
  187.                 ((int)barr[ start + 3 ] & 0xFF )
  188.             );
  189.         return ret;
  190.     }
  191. }